﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace XueMeng.DAL
{
	/// <summary>
	/// 数据访问类:AuctionService
	/// </summary>
	public partial class AuctionService
	{
		public AuctionService()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("A_Id", "Auction"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int A_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Auction");
			strSql.Append(" where A_Id=@A_Id");
			SqlParameter[] parameters = {
					new SqlParameter("@A_Id", SqlDbType.Int,4)
			};
			parameters[0].Value = A_Id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(XueMeng.Model.Auction model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Auction(");
			strSql.Append("T_Id,E_Id,Fraction,CreateDate,FinishDate,Solution,Enable)");
			strSql.Append(" values (");
			strSql.Append("@T_Id,@E_Id,@Fraction,@CreateDate,@FinishDate,@Solution,@Enable)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@T_Id", SqlDbType.Int,4),
					new SqlParameter("@E_Id", SqlDbType.Int,4),
					new SqlParameter("@Fraction", SqlDbType.Int,4),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@FinishDate", SqlDbType.Int,4),
					new SqlParameter("@Solution", SqlDbType.Text),
					new SqlParameter("@Enable", SqlDbType.Int,4)};
			parameters[0].Value = model.T_Id;
			parameters[1].Value = model.E_Id;
			parameters[2].Value = model.Fraction;
			parameters[3].Value = model.CreateDate;
			parameters[4].Value = model.FinishDate;
			parameters[5].Value = model.Solution;
			parameters[6].Value = model.Enable;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
        ///// <summary>
        ///// 更新一条数据
        ///// </summary>
        //public bool Update(XueMeng.Model.Auction model)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("update Auction set ");
        //    strSql.Append("T_Id=@T_Id,");
        //    strSql.Append("E_Id=@E_Id,");
        //    strSql.Append("Fraction=@Fraction,");
        //    strSql.Append("CreateDate=@CreateDate,");
        //    strSql.Append("FinishDate=@FinishDate,");
        //    strSql.Append("Solution=@Solution,");
        //    strSql.Append("Enable=@Enable");
        //    strSql.Append(" where A_Id=@A_Id");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@T_Id", SqlDbType.Int,4),
        //            new SqlParameter("@E_Id", SqlDbType.Int,4),
        //            new SqlParameter("@Fraction", SqlDbType.Int,4),
        //            new SqlParameter("@CreateDate", SqlDbType.DateTime),
        //            new SqlParameter("@FinishDate", SqlDbType.Int,4),
        //            new SqlParameter("@Solution", SqlDbType.Text),
        //            new SqlParameter("@Enable", SqlDbType.Int,4),
        //            new SqlParameter("@A_Id", SqlDbType.Int,4)};
        //    parameters[0].Value = model.T_Id;
        //    parameters[1].Value = model.E_Id;
        //    parameters[2].Value = model.Fraction;
        //    parameters[3].Value = model.CreateDate;
        //    parameters[4].Value = model.FinishDate;
        //    parameters[5].Value = model.Solution;
        //    parameters[6].Value = model.Enable;
        //    parameters[7].Value = model.A_Id;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(XueMeng.Model.Auction model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Auction set ");
            strSql.Append("Fraction=@Fraction,");
            strSql.Append("FinishDate=@FinishDate,");
            strSql.Append("Solution=@Solution,");
            strSql.Append(" where A_Id=@A_Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Fraction", SqlDbType.Int,4),
					new SqlParameter("@FinishDate", SqlDbType.Int,4),
					new SqlParameter("@Solution", SqlDbType.Text),
					new SqlParameter("@A_Id", SqlDbType.Int,4)};
            parameters[1].Value = model.Fraction;
            parameters[2].Value = model.FinishDate;
            parameters[3].Value = model.Solution;
            parameters[4].Value = model.A_Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int A_Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Auction ");
			strSql.Append(" where A_Id=@A_Id");
			SqlParameter[] parameters = {
					new SqlParameter("@A_Id", SqlDbType.Int,4)
			};
			parameters[0].Value = A_Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string A_Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Auction ");
			strSql.Append(" where A_Id in ("+A_Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XueMeng.Model.Auction GetModel(int A_Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 A_Id,T_Id,E_Id,Fraction,CreateDate,FinishDate,Solution,Enable from Auction ");
			strSql.Append(" where A_Id=@A_Id");
			SqlParameter[] parameters = {
					new SqlParameter("@A_Id", SqlDbType.Int,4)
			};
			parameters[0].Value = A_Id;

			XueMeng.Model.Auction model=new XueMeng.Model.Auction();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["A_Id"]!=null && ds.Tables[0].Rows[0]["A_Id"].ToString()!="")
				{
					model.A_Id=int.Parse(ds.Tables[0].Rows[0]["A_Id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["T_Id"]!=null && ds.Tables[0].Rows[0]["T_Id"].ToString()!="")
				{
					model.T_Id=int.Parse(ds.Tables[0].Rows[0]["T_Id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["E_Id"]!=null && ds.Tables[0].Rows[0]["E_Id"].ToString()!="")
				{
					model.E_Id=int.Parse(ds.Tables[0].Rows[0]["E_Id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Fraction"]!=null && ds.Tables[0].Rows[0]["Fraction"].ToString()!="")
				{
					model.Fraction=int.Parse(ds.Tables[0].Rows[0]["Fraction"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CreateDate"]!=null && ds.Tables[0].Rows[0]["CreateDate"].ToString()!="")
				{
					model.CreateDate=DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["FinishDate"]!=null && ds.Tables[0].Rows[0]["FinishDate"].ToString()!="")
				{
					model.FinishDate=int.Parse(ds.Tables[0].Rows[0]["FinishDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Solution"]!=null && ds.Tables[0].Rows[0]["Solution"].ToString()!="")
				{
					model.Solution=ds.Tables[0].Rows[0]["Solution"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Enable"]!=null && ds.Tables[0].Rows[0]["Enable"].ToString()!="")
				{
					model.Enable=int.Parse(ds.Tables[0].Rows[0]["Enable"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select A_Id,T_Id,E_Id,Fraction,CreateDate,FinishDate,Solution,Enable ");
			strSql.Append(" FROM Auction ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" A_Id,T_Id,E_Id,Fraction,CreateDate,FinishDate,Solution,Enable ");
			strSql.Append(" FROM Auction ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Auction ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.A_Id desc");
			}
			strSql.Append(")AS Row, T.*  from Auction T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Auction";
			parameters[1].Value = "A_Id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

